// 0 引入发送请求的封装方法
import { request } from '../../request/index'
Page({
  data: {
    swiperList: [],    //轮播图数组
    catesList:[],   //导航分类数组
    floorList:[],    //楼层数据
  },
  //页面开始加载就会触发
  onLoad: function () {
    this.getSwiperList();
    this.getCatesList();
    this.getFloorList();

  },


  //获取轮播图数据
  getSwiperList(){
    //1.发送异步请求获取轮播图数据
    request({
      url: '/home/swiperdata' 
    })
    .then((result) => {
      let results = result;   
      // 替换返回的轮播图跳转路径
      const swiperURLArr = ["/pages/goods_detail/index?goods_id=129","/pages/goods_detail/index?goods_id=395","/pages/goods_detail/index?goods_id=38"]
      for(let i in results){  
        results[i].navigator_url= swiperURLArr[i]
      }
      this.setData({  //拿轮播图数据
        swiperList: results,
      })
    })
  },

  //获取导航分类数据
  getCatesList(){
    request({
      url: '/home/catitems' 
    })
    .then((result) => {
      // console.log(result);
      this.setData({  
        catesList: result
      })
    })
  },

  //获取楼层数据
  getFloorList(){
    request({
      url: '/home/floordata' 
    })
    .then((result) => {      //replace()字符串中某些字符替换某些字符，或替换一个与正则表达式匹配的子串。
      let results = JSON.parse(JSON.stringify(result).replace(/\?query/g,"/index?query"));  
      this.setData({  
        floorList: results
      })
    })
  }
});